package com.atguigu.flink.chapter11;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2022/1/22 13:50
 */
public class Flink09_Time_Event_1 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
    
      
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        // sql中时间属性, 必须是时间戳类型, 需要把ts转成一个时间戳 timestamp(3)
        // bigint     ->        timestamp(3)
        tEnv.executeSql("create table sensor(" +
                            "   id string, " +
                            "   ts bigint, " +
                            "   vc int," +
                            "   et as TO_TIMESTAMP_LTZ(ts, 3)," +
                            "   watermark for et as et - interval '3' second " +
                            ")with(" +
                            "   'connector' = 'filesystem', " +
                            "   'path' = 'input/sensor.txt', " +
                            "   'format' = 'csv' " +
                            ")");
    
        Table table = tEnv.sqlQuery("select * from sensor");
        table.printSchema();
        table.execute().print();
    }
}
